﻿using System;
using System.ComponentModel;
using System.Diagnostics;

namespace TaskManagerApp.Model
{

    public class Task : ITask, IDataErrorInfo
    {
        protected Task()
        {
        }

        public static ITask CreateTask()
        {
            return new Task { TaskType = Strings.TaskViewModel_TaskType_General};
        }

        #region Implementation of ITask

        public string Description
        {
            get;
            set;
        }

        public DateTime Start
        {
            get;
            set;
        }

        public DateTime End
        {
            get;
            set;
        }

        public string TaskType
        {
            get;
            set;
        }

        #endregion

        #region Implementation of IDataErrorInfo

        string IDataErrorInfo.Error { get { return null; } }

        string IDataErrorInfo.this[string propertyName]
        {
            get { return GetValidationError(propertyName); }
        }


        #endregion

        #region Validation
        //Primitive Validation method
        //Validations can be moved into another partial class or 
        //can be handled outside the model
        //But just for the scope of this test its left here in the model

        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (!isNullOrEmpty(GetValidationError(property)))
                        return false;

                return true;
            }
        }

        static readonly string[] ValidatedProperties = 
        { 
            Constants.Description, 
            Constants.Start, 
            Constants.End,
            Constants.TaskType
        };


        static bool isNullOrEmpty(string value)
        {
            return
                String.IsNullOrEmpty(value) ||
                value.Trim() == String.Empty;
        }

        private string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            
            switch (propertyName)
            {
                case Constants.Description:
                    error = ValidateDescription();
                    break;

                case Constants.Start:
                    error = ValidateStartDate();
                    break;

                case Constants.End:
                    error = ValidateEndDate();
                    break;

                case Constants.TaskType:
                    error = ValidateTaskType();
                    break;
                    
                default:
                    Debug.Fail("Unexpected property for Task: " + propertyName);
                    break;
            }

            return error;
        }

        private string ValidateTaskType()
        {
            if (string.IsNullOrEmpty(TaskType))
                return Errors.Description_Empty_Error;
            
            if (TaskType == Strings.TaskViewModel_TaskType_General)
                return Strings.TaskViewModel_Error_InvalidTaskType;

            return string.Empty;
        }


        private string ValidateEndDate()
        {
            if (End == DateTime.MinValue)
                return Errors.EndDate_Null_Error;
            
            return string.Empty;
        }

        private string ValidateStartDate()
        {
            if (Start == DateTime.MinValue)
                return Errors.StartDate_Null_Error;

            return string.Empty;
        }

        private string ValidateDescription()
        {
            if (isNullOrEmpty(Description))
                return Errors.Description_Empty_Error;

            return string.Empty;
        }

        #endregion

    }
}
